<script setup lang="ts">
import { ref } from "vue";
import { useConversationRecordHook } from "./utils/hook";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { CommonUtils } from "@/utils/common";
import ConversationRecordFormModal from "./conversation-record-form-modal.vue";

import Delete from "@iconify-icons/ep/delete";
import Search from "@iconify-icons/ep/search";
import Refresh from "@iconify-icons/ep/refresh";
import AddFill from "@iconify-icons/ri/add-circle-line";
import EditPen from "@iconify-icons/ep/edit-pen";

defineOptions({
  name: "ConversationRecord"
});

const tableRef = ref();
const searchFormRef = ref();

const {
  searchFormParams,
  pageLoading,
  columns,
  dataList,
  pagination,
  timeRange,
  defaultSort,
  multipleSelection,
  onSearch,
  resetForm,
  onSortChanged,
  exportAllExcel,
  getConversationRecordList,
  handleDelete,
  handleBulkDelete
} = useConversationRecordHook();

const opType = ref<"add" | "update">("add");
const modalVisible = ref(false);
const opRow = ref();

function openDialog(type: "add" | "update", row?) {
  opType.value = type;
  opRow.value = row;
  modalVisible.value = true;
}

// 在原有代码后添加房间类型映射
const roomTypeOptions = [
  { value: "1", label: "外部群" },
  { value: "2", label: "外部联系人" },
  { value: "3", label: "内部群" },
  { value: "4", label: "内部联系人" }
];

const roomTypeMap = Object.fromEntries(
  roomTypeOptions.map(item => [item.value, item.label])
);
</script>

<template>
  <div class="main">
    <el-form
      ref="searchFormRef"
      :inline="true"
      :model="searchFormParams"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <!-- 保留原有发言内容查询框 -->
      <el-form-item label="发言内容" prop="spoken">
        <el-input
          v-model="searchFormParams.spoken"
          placeholder="请输入发言内容"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>

      <!-- 保留原有接收者查询框 -->
      <el-form-item label="接收者" prop="receivedName">
        <el-input
          v-model="searchFormParams.receivedName"
          placeholder="请输入接收者"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>

      <!-- 新增群组名称查询框 -->
      <el-form-item label="群组名称" prop="groupName">
        <el-input
          v-model="searchFormParams.groupName"
          placeholder="请输入群组名称"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>

      <!-- 新增房间类型下拉框 -->
      <el-form-item label="房间类型" prop="roomType">
        <el-select
          v-model="searchFormParams.roomType"
          placeholder="请选择房间类型"
          clearable
          class="!w-[200px]"
        >
          <el-option
            v-for="item in roomTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <!-- 新增回答内容查询框 -->
      <el-form-item label="回答内容" prop="answer">
        <el-input
          v-model="searchFormParams.answer"
          placeholder="请输入回答内容"
          clearable
          class="!w-[200px]"
        />
      </el-form-item>

      <!-- 保留原有创建时间查询框 -->
      <el-form-item label="创建时间">
        <el-date-picker
          class="!w-[240px]"
          v-model="timeRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>

      <!-- 保留原有搜索和重置按钮 -->
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="pageLoading"
          @click="onSearch(tableRef)"
        >
          搜索
        </el-button>
        <el-button
          :icon="useRenderIcon(Refresh)"
          @click="resetForm(searchFormRef, tableRef)"
        >
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <!-- 表格部分保持不变 -->
    <PureTableBar title="会话记录列表" :columns="columns" @refresh="onSearch">
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog('add')"
        >
          新增记录
        </el-button>
        <el-button
          type="danger"
          :icon="useRenderIcon(Delete)"
          @click="handleBulkDelete(tableRef)"
        >
          批量删除
        </el-button>
        <el-button
          type="primary"
          @click="CommonUtils.exportExcel(columns, dataList, '会话记录')"
        >
          单页导出
        </el-button>
        <el-button type="primary" @click="exportAllExcel">全部导出</el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          border
          ref="tableRef"
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="pageLoading"
          :size="size"
          adaptive
          :data="dataList"
          :columns="dynamicColumns"
          :pagination="pagination"
          :paginationSmall="size === 'small'"
          @page-size-change="getConversationRecordList"
          @page-current-change="getConversationRecordList"
          @sort-change="onSortChanged"
          @selection-change="
            rows => (multipleSelection = rows.map(item => item.id))
          "
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('update', row)"
            >
              修改
            </el-button>
            <el-popconfirm
              :title="`是否确认删除ID为${row.id}的这条记录`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>

    <ConversationRecordFormModal
      :type="opType"
      v-model="modalVisible"
      :row="opRow"
      @success="getConversationRecordList"
    />
  </div>
</template>
